package band.wukong.practice.webservice.kh.sample11.nongen;

import band.wukong.practice.webservice.kh.sample11.RobotService;
import band.wukong.practice.webservice.kh.sample11.RobotServiceImplService;
import band.wukong.practice.webservice.kh.sample11.SayHi2;
import band.wukong.practice.webservice.kh.sample11.SayHi2Response;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.testng.Assert;
import org.testng.annotations.Test;

import javax.xml.ws.handler.Handler;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wukong(wukonggg@139.com)
 */
public class ClientTest {

    @Test
    public void sayHiByJaxws() {
        RobotServiceImplService service = new RobotServiceImplService();
        RobotService port = service.getRobotServiceImplPort();

        String echo = port.sayHi("CXF");
        Assert.assertEquals(echo, "Hi, CXF! ");
    }

    @Test
    public void sayHiByCxf() {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress(RobotConst.SERVICE_URL);
        factory.setServiceClass(RobotService.class);

        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());

        RobotService port = (RobotService) factory.create();
        String echo = port.sayHi("CXF");
        Assert.assertEquals(echo, "Hi, CXF! ");
    }

    @Test
    public void sayHi2() {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress(RobotConst.SERVICE_URL);
        factory.setServiceClass(RobotService.class);

        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());

        RobotService port = (RobotService) factory.create();
        SayHi2 sayHi2 = new SayHi2();
        sayHi2.setName("CXF");
        SayHi2Response resp = port.sayHi2(sayHi2, "admin");
        Assert.assertEquals(resp.getHi(), "Hi, CXF! ");
    }

    @Test
    public void sayHi3ByHandler() {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress(RobotConst.SERVICE_URL);
        factory.setServiceClass(RobotService.class);

        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());

        List<Handler> handlers = new ArrayList<>();
        handlers.add(new LicenseHandler());
        factory.setHandlers(handlers);

        RobotService port = (RobotService) factory.create();
        String echo = port.sayHi3("CXF");
        Assert.assertEquals(echo, "Hi, CXF! ");
    }

}
